package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.bes.sdk.utils.SettingsJsonUtils;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.lib.devices.sgsettingentity.setting.PanelModel;
import bb.lanxing.lib.devices.sgsettingentity.setting.PanelSportMode;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.ui.GsonUtils;
import bb.lanxing.utils.PanelModelToJsonUtil;

public class DeviceRemindActivity extends BaseActivity {
    private static final int REQUEST_CODE_TARGET = 2563;

    private TextView intervalReminderValue;
    private Switch intervalReminderSwitch;
    private TextView hrIntervalValue;
    private Switch hrIntervalSwitch;
    private TextView speedIntervalValue;
    private Switch speedIntervalSwitch;

    private String device_model = "BC270";
    private String sn = "test";
    private PanelModel PANEL_MODEL;
    private PanelSportMode panelSportMode;
    private int sportMode;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_remind);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView title = findViewById(R.id.title);
        title.setText(getIntent().getStringExtra("title"));

        sportMode = getIntent().getIntExtra("sportMode", 0);
        PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(getIntent().getStringExtra("PANEL_MODEL"), PanelModel.class);

        CardView intervalReminderLayout = findViewById(R.id.intervalReminderLayout);
        intervalReminderLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceReminderActivity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_remind_interval_reminder));
            intent.putExtra("reminderType", 0);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        intervalReminderValue = findViewById(R.id.intervalReminderValue);
        intervalReminderSwitch = findViewById(R.id.intervalReminderSwitch);
        intervalReminderSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                panelSportMode.getRemind().set(0, 1);
            } else {
                panelSportMode.getRemind().set(0, 0);
            }
        });

        CardView hrIntervalLayout = findViewById(R.id.hrIntervalLayout);
        hrIntervalLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceReminder1Activity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_remind_hr_interval));
            intent.putExtra("reminderType", 1);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        hrIntervalValue = findViewById(R.id.hrIntervalValue);
        hrIntervalSwitch = findViewById(R.id.hrIntervalSwitch);
        hrIntervalSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                panelSportMode.getRemind().set(1, 1);
            } else {
                panelSportMode.getRemind().set(1, 0);
            }
        });

        CardView speedIntervalLayout = findViewById(R.id.speedIntervalLayout);
        speedIntervalLayout.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeviceReminderActivity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_remind_speed_interval));
            intent.putExtra("reminderType", 2);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_TARGET);
        });
        speedIntervalValue = findViewById(R.id.speedIntervalValue);
        speedIntervalSwitch = findViewById(R.id.speedIntervalSwitch);
        speedIntervalSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                panelSportMode.getRemind().set(2, 1);
            } else {
                panelSportMode.getRemind().set(2, 0);
            }
        });

        updateData();
    }

    private void updateData() {
        if (PANEL_MODEL == null) {
            return;
        }
        panelSportMode = PANEL_MODEL.getProfilesList().get(sportMode);
        intervalReminderValue.setText(panelSportMode.getRemind().get(3) + getString(R.string.device_profiles_setting_detail_target_time_title));
        intervalReminderSwitch.setChecked(panelSportMode.getRemind().get(0) == 1);
        hrIntervalValue.setText(panelSportMode.getRemind().get(4) + "-" + panelSportMode.getRemind().get(5)
                + getString(R.string.device_profiles_setting_detail_target_hr_title));
        hrIntervalSwitch.setChecked(panelSportMode.getRemind().get(1) == 1);
        speedIntervalValue.setText(panelSportMode.getRemind().get(6) + getString(R.string.device_profiles_setting_detail_target_speed_title));
        speedIntervalSwitch.setChecked(panelSportMode.getRemind().get(2) == 1);
    }

    @Override
    public void onBackPressed() {
        String content = PanelModelToJsonUtil.toJson(PANEL_MODEL);
        device_model = SettingsJsonUtils.getDeviceModel(this);
        sn = SettingsJsonUtils.getSn(this);
        CommonUtil.saveDeviceProfile(content, device_model, sn);
        CommonUtil.sendPanelsJsonFile(this, device_model, sn);
        Intent intent = new Intent();
        intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_TARGET) {
            PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(data.getStringExtra("PANEL_MODEL"), PanelModel.class);
            updateData();
        }
    }
}